
var globalData = getApp().globalData;
var httpsUrl = getApp().globalData.httpsUrl;
function formatDate(now, is_nedd_hms) {
  var result = "";
  var year = now.getYear();
  var month = now.getMonth() + 1;
  if (month < 10) {
    month = "0" + month;
  }
  var date = now.getDate();
  if (date < 10) {
    date = "0" + date;
  }
  var hour = now.getHours();
  var minute = now.getMinutes();
  var second = now.getSeconds();
  if (is_nedd_hms == 1) {
    result = "20" + (year - 100) + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
  }
  if (is_nedd_hms == 0) {
    result = "20" + (year - 100) + "-" + month + "-" + date;
  }
  return result;
}

function getNowTime() {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var day = now.getDate();
  if (month < 10) {
    month = '0' + month;
  };
  if (day < 10) {
    day = '0' + day;
  };
  //  如果需要时分秒，就放开
  // var h = now.getHours();
  // var m = now.getMinutes();
  // var s = now.getSeconds();
  var formatDate = year + '-' + month + '-' + day;
  return formatDate;
}

//判断值是否为空
function isEmpty(value){
  if (value == undefined) {
    return true;
  }
  if (value == null) {
    return true;
  }
  if (typeof (value) == "string") {
    if (value.trim() == "") {
      return true;
    }
  }
  return false;

}

function showSuccessToast(text){
  wx.showToast({
    title: text,
    duration: 1500
  })
}

function showErrorToast(text) {
    wx.showToast({
      title: text,
      // image: 'https://m.chepaidang.cc/xcxImg/image/login/error@2x.png',
      duration: 2000
    })
}

function showModal(content){
  wx.showModal({
    title: '提示',
    content:content,
    showCancel:false,
    confirmColor:"#fc6d04"
  })
}

function showLoading(text){
  wx.showLoading({
    title: text,
    mask:true
  })
}

function hideLoading(){
  wx.hideLoading();
}

//商品图片，返回第一张
function getImg(imgs) {
  let img = globalData.imgUrl + "/homepage/moren.png";
  if (imgs != null && imgs != "" && imgs != undefined){
    if (imgs.indexOf(",") == -1){
      img = globalData.imgUrlApi + imgs;//若无逗号，则只有一张
    }else{
      imgs = imgs.split(",");
      img = globalData.imgUrlApi + imgs[0];
    }
  }
  return img;
}
//获取openid
function getOpenId() {
  wx.login({
    success: function (res) {
      wx.request({
        header: { "Cookie": getApp().getJSESSIONID(), "content-Type": "application/x-www-form-urlencoded" },
        url: httpsUrl + "/payCtrl/getOpenId",
        dataType: "json",
        method: "POST",
        data: { "code": res.code },
        success: function (res) {
          console.log("获取openid返回:" + JSON.stringify(res));
          res = res.data;
          if (res.code == 0) {
            let data = JSON.parse(res.data);
            console.log("openid:" + data.openid);
          } else {
            console.log("获取openid失败：" + res.msg);
          }
        }
      })
    }
  })
}
//根据授权所得手机号，更新用户手机号码
function updatePhoneByWechat(phone) {
  let openId = wx.getStorageSync("openidKey");
  var params = new Object();
  params.phone = phone;
  wx.showLoading({ title: '请稍等...', })
    wx.request({
      header: { "openId": openId ,"content-Type": "application/x-www-form-urlencoded"},
      url: httpsUrl + "/member/updateInfo",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("更新手机号:" + JSON.stringify(res));
        if (res.code == 0) {
        }
      }
    })
}
//用户打开小程序就调用下，要先获取几个code，so不能直接onLaunch
function openXcx() {
  let parentCode = wx.getStorageSync("parentCode");
  if (parentCode != undefined && parentCode != "undefined" && parentCode != null && parentCode.length>0){
    let lat = '';
    let lon = '';
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log("已获取到经纬度");
        lat = res.latitude;
        lon = res.longitude;
      }
    })
    setTimeout(function () {
      openXcx2(lat,lon);
    }, 5000) //延迟执行 1000为1秒
  }
}
function openXcx2(lat,lon) {
  console.log(lat+","+lon);
  let parentCode = wx.getStorageSync("parentCode");
    let openId = wx.getStorageSync("openidKey");
    var params = new Object();
    params.memberCode = parentCode;
    params.posterCode = wx.getStorageSync("posterCode");
    params.businessCode = wx.getStorageSync("businessCode");
    params.lon = lon;
    params.lat = lat;
    console.log("openXcx参数："+JSON.stringify(params));
    wx.showLoading({ title: '请稍等...', })
    wx.request({
      header: { "openId": openId,},
      url: httpsUrl + "/member/openXcx",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("openXcx返回:" + JSON.stringify(res));
        if (res.result == 0) {
        }
      }
    })
}
module.exports={
  formatDate:formatDate,
  isEmpty: isEmpty,
  showSuccessToast: showSuccessToast,
  showErrorToast: showErrorToast,
  showModal:showModal,
  showLoading: showLoading,
  hideLoading: hideLoading,
  getImg: getImg,
  getOpenId: getOpenId,
  updatePhoneByWechat: updatePhoneByWechat,
  openXcx: openXcx,
}


